package com.example.shaoying.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.common.Constants;
import com.example.shaoying.yuejian.javabean.TrainInfo;
import com.example.shaoying.yuejian.model.ClubeModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


/**
 * Created by Administrator on 2017/10/10.
 */

public class TrainActivity extends AppCompatActivity {
    private Button join;
    private String t_id;
    private ImageView pic;
    private String train_name,train_price;
    private TextView train_detail_title,train_detail_intro,train_detail_notify,
            train_detail_time,train_detail_address,train_detail_price;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.traindetail);
        init();
        event();
        conn();
    }

    private void conn() {
        ClubeModel model=new ClubeModel();
        Call<TrainInfo> call=model.getTtainInfo(t_id);
        call.enqueue(new Callback<TrainInfo>() {
            @Override
            public void onResponse(Call<TrainInfo> call, Response<TrainInfo> response) {
                TrainInfo info=response.body();
                train_price=info.getPrice();
                train_detail_price.setText("￥"+info.getPrice());
                train_name=info.getName();
                train_detail_title.setText(info.getName());
                train_detail_intro.setText(info.getIntro());
                String notice=info.getNotice().replaceAll("<p>","").replaceAll("</p>","");
                train_detail_notify.setText(notice);
                train_detail_time.setText(info.getTraintime());
                train_detail_address.setText(info.getPlace());
                Picasso.with(TrainActivity.this).load(Constants.PIC_URL+info.getThumb()).into(pic);
            }

            @Override
            public void onFailure(Call<TrainInfo> call, Throwable t) {
                Toast.makeText(TrainActivity.this,"详情获取失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void event() {
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(TrainActivity.this,TrainJoinActivity.class);
                intent.putExtra("train_id",t_id);
                intent.putExtra("train_name",train_name);
                intent.putExtra("train_price",train_price);

                startActivity(intent);
            }
        });
    }

    private void init()
    {
        pic=(ImageView)findViewById(R.id.train_detail_pic);
        train_detail_title=(TextView)findViewById(R.id.train_detail_title);
        train_detail_time=(TextView)findViewById(R.id.train_detail_time);
        train_detail_notify=(TextView)findViewById(R.id.train_detail_notify);
        train_detail_address=(TextView)findViewById(R.id.train_detail_address);
        train_detail_intro=(TextView)findViewById(R.id.train_detail_intro);
        train_detail_price=(TextView)findViewById(R.id.train_detail_price);
        join=(Button)findViewById(R.id.train_detail_join);
        Intent intent=getIntent();
        t_id=intent.getStringExtra("train_id");
    }
}
